package com.car.bag;

import java.sql.SQLException;
import java.util.ArrayList;

import com.wzsck.util.DBManager;
import com.wzsck.util.QueryDataResult;

public class CarClassManager {

	public ArrayList<carclass> query(int page, int limit) {
		ArrayList<carclass> list = new ArrayList<carclass>();
		String sql = "select * from wzs_class ";
		if (page != 0 && limit != 0) {
			sql += " limit " + (page - 1) * limit + "," + limit;
		}

		QueryDataResult qdr = DBManager.executeQuery(sql);
		if (qdr.rs != null) {
			try {
				while (qdr.rs.next()) {
					int carclassid = qdr.rs.getInt("classid");
					String carclassname=qdr.rs.getString("classname");
					carclass c = new carclass(carclassid,carclassname);
					list.add(c);
					
        		}
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

		}
		qdr.close();
		return list;
	}
	//============================
	//add
			public int add( String classname) {
				String sql = String.format(
						"insert into wzs_class(`classname`) values('%s')",classname);
				
				System.out.print(sql);
				int i = DBManager.executeUpdate(sql);
				return i;

			}
			
		
			public int update(String classid, String classname) {
				String sql = String.format(
						" update wzs_class set `classname`='%s' where classid = %s ",
						classname,classid);
				System.out.print(sql);
				int i = DBManager.executeUpdate(sql);
				return i;
			}
			public int del(int id) {
				String sql = "delete from wzs_class where classid = " + id;
				System.out.print(sql);
				int i = DBManager.executeUpdate(sql);
				return i;
			}
	//===============================================================		
	 public int count() {//count查非空
	 		String sql="select count(*) from wzs_class";//换表名
	 		QueryDataResult qdr=DBManager.executeQuery(sql);
	 		if (qdr.rs!=null) {
	 			try {
	 				while (qdr.rs.next()) {
	 					int count =qdr.rs.getInt("count(*)");
	 					return count;
	 				}
	 			} catch (SQLException e) {
	 				// TODO Auto-generated catch block
	 				e.printStackTrace();
	 			}
	 		}
	 		return 0;
	 	}
}
